﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QWeiBo.ViewModel;
using TencentWeiboSDK.Services;
using TencentWeiboSDK.Model;
using System.Collections.ObjectModel;
using System.Threading;
using TencentWeiboSDK.Services.Util;

namespace QWeiBoN.ViewModule
{
    /// <summary>
    /// 用于得到某条微博和微博对应的评论列表
    /// </summary>
    public class WeiboCommentsModel : BaseViewModel
    {
        /// <summary>
        /// 实例化时间线相关 API 服务.
        /// </summary>
        private StatusesService statusService = new StatusesService();

        /// <summary>
        /// 构造函数
        /// </summary>
        public WeiboCommentsModel()
        {
            Singleline = new ObservableCollection<Status>();
        }

        /// <summary>
        /// 用于进行微博的收藏和取消收藏
        /// </summary>
        /// <param name="weiboId"></param>
        /// <param name="isAdd"></param>
        /// <param name="action"></param>
        public void FavoriteWeibo(string weiboId, bool isAdd, Action<Callback<Result>> action)
        {
            // 异步操作，可以提高用户体验
            new Thread(() =>
            {
                statusService.FavoriteWeiboFromId(new ServiceArgument() { Id = weiboId }, isAdd, (callback) =>
                {
                    if (null != action)
                    {
                        action(callback);
                    }
                });
            });
        }

        /// <summary>
        /// 用于根据微博id得到微博详细信息
        /// </summary>
        /// <param name="weiboId"></param>
        /// <param name="action"></param>
        public void getWeiboDetail(string weiboId, Action<Callback<Status>> action)
        {
            // 异步操作，用于增加用户体验
            new Thread(() =>
            {
                statusService.SingleWeiboDetail(new ServiceArgument() { Id = weiboId }, (callback) =>
                {
                    // 切换并回调到UI线程.
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        if (null != action)
                        {
                            action(callback);
                        }
                    });
                });
            }).Start();
        }

        /// <summary>
        /// 得到微博和对应的评论列表
        /// </summary>
        /// <param name="weiboId"></param>
        /// <param name="action"></param>
        public void getCommentList(string weiboId,int flag, int reqnum,long pagetime, Action<Callback<StatusCollection>> action)
        {
             // 异步刷新可以让用户有更好的操作体验
            new Thread(() =>
            {
                statusService.SingleCommentTimelineIds(new ServiceArgument() { Rootid = weiboId, Flag = 2, PageFlag = flag, Reqnum = reqnum, PageTime = pagetime }, (callBack) =>
                {
                    // 因为回调回来之后会是后台线程，需要切换到UI线程去处理ObservableCollection
                    // 相关的操作
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        //Singleline.Clear();
                        //callBack.Data.ForEach(a =>
                        //{
                        //    //a.Nick = a.Nick + "：" + a.Text;
                        //    DateTime date = new DateTime(1970, 01, 01, 00, 00, 00);
                        //    date = date.AddSeconds(a.Timestamp);
                        //    a.From = date.Month + "月" + date.Day + "日 " + date.Hour + ":" + date.Minute + "  " + a.From;
                        //    if (null == a.Head || a.Head.Equals(""))
                        //    {
                        //        a.Head = "Resources/head_50.jpg";
                        //    }
                        //    Singleline.Add(a);
                        //});

                        // 切换并回调到UI线程.
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (null != action)
                            {
                                action(callBack);
                            }
                        });
                    });
                });
            }).Start();
        }

        /// <summary>
        /// 用于清除历史记录
        /// </summary>
        public void clearHistory()
        {
            Singleline.Clear();
        }

        /// <summary>
        /// 获取或设置首页时间线
        /// </summary>
        public ObservableCollection<Status> Singleline
        {
            get;
            set;
        }
    }
}
